package net.kleinhenz.norabase.client.datasource;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.grid.ListGridRecord;

import net.kleinhenz.norabase.shared.SortOrientation;
import net.kleinhenz.norabase.shared.TextMatchStyle;
import net.kleinhenz.norabase.shared.model.Champion;
import net.kleinhenz.norabase.shared.model.Equipment;
import net.kleinhenz.norabase.shared.model.Relic;
import net.kleinhenz.norabase.shared.model.Rune;
import net.kleinhenz.norabase.shared.model.Spell;

public abstract class RuneDS extends GwtRpcDataSource {

    public RuneDS () {
    	
        DataSourceField field;
        field = new DataSourceIntegerField ("id", "Id");
        field.setPrimaryKey (true);
        field.setRequired(false);
        field.setCanEdit(false);
        field.setHidden(true);
        addField(field);
        
        field = new DataSourceTextField ("name", "Name");
        field.setRequired(true);
        field.setCanEdit(false);
        addField(field);

        field = new DataSourceBooleanField("allowRanked", "Ranked allowed");
        field.setRequired(true);
        field.setCanEdit(false);
        field.setHidden(true);
        addField(field);

        field = new DataSourceTextField ("expansion", "Expansion");
        field.setRequired(true);
        field.setCanEdit(false);
        addField(field);

        field = new DataSourceTextField ("faction", "Faction");
        field.setRequired(true);
        field.setCanEdit(false);
        addField(field);

        field = new DataSourceTextField ("artist", "Artist");
        field.setRequired(true);
        field.setCanEdit(false);
        addField(field);

        field = new DataSourceIntegerField ("nora", "Nora");
        field.setRequired(true);
        field.setCanEdit(false);
        addField(field);

        field = new DataSourceIntegerField ("rarity", "Rarity");
        field.setRequired(true);
        field.setCanEdit(false);
        addField(field);

        field = new DataSourceIntegerField ("owned", "Owned");
        field.setRequired(true);
        field.setCanEdit(true);
        addField(field);
        
        field = new DataSourceIntegerField ("type", "Type");
        field.setRequired(true);
        field.setCanEdit(false);
        field.setCanFilter(false);
        addField(field);
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
    	throw new RuntimeException("Not Implemented.");
    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
    	throw new RuntimeException("Not Implemented.");
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
    	throw new RuntimeException("Not Implemented.");
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
    	throw new RuntimeException("Not Implemented.");
    }

    protected void copyValues(ListGridRecord from, Rune to) {
    	to.setId(from.getAttributeAsInt("id"));
    	to.setName(from.getAttributeAsString("name"));
    	to.setAllowRanked(from.getAttributeAsBoolean("allowRanked"));
    	to.setExpansion(from.getAttributeAsString("expansion"));
    	to.setFaction(from.getAttributeAsString("faction"));
    	to.setArtist(from.getAttributeAsString("artist"));
    	to.setNora(from.getAttributeAsInt("nora"));
    	to.setRarity(from.getAttributeAsInt("rarity"));
    	to.setOwned(from.getAttributeAsInt("owned"));
    }

	protected void copyValues(Rune from, ListGridRecord to) {

		to.setAttribute("id", from.getId());
        to.setAttribute("name", from.getName ());
		to.setAttribute("allowRanked", from.isAllowRanked());
		to.setAttribute("expansion", from.getExpansion());
		to.setAttribute("faction", from.getFaction());
		to.setAttribute("artist", from.getArtist());
		to.setAttribute("nora", from.getNora());
		to.setAttribute("rarity", from.getRarity());		
		to.setAttribute("owned", from.getOwned());
		if (from instanceof Champion)
			to.setAttribute("type", Rune.CHAMPION);
		else if (from instanceof Equipment)
			to.setAttribute("type", Rune.EQUIPMENT);
		else if (from instanceof Spell)
			to.setAttribute("type", Rune.SPELL);
		else if (from instanceof Relic)
			to.setAttribute("type", Rune.RELIC);
	}

    protected ListGridRecord getEditedRecord (DSRequest request) {
        // Retrieving values before edit
        JavaScriptObject oldValues = request.getAttributeAsJavaScriptObject ("oldValues");
        // Creating new record for combining old values with changes
        ListGridRecord newRecord = new ListGridRecord ();
        // Copying properties from old record
        JSOHelper.apply (oldValues, newRecord.getJsObj ());
        // Retrieving changed values
        JavaScriptObject data = request.getData ();
        // Apply changes
        JSOHelper.apply (data, newRecord.getJsObj ());
        return newRecord;
    }

	protected Map<String, SortOrientation> convertSortSpecifier(com.smartgwt.client.data.SortSpecifier[] sortSpecifiers) {

		Map<String, SortOrientation> sp = new HashMap<String, SortOrientation>();
		if (sortSpecifiers!=null)
			for (com.smartgwt.client.data.SortSpecifier sortSpecifier : sortSpecifiers) {
				sp.put(sortSpecifier.getField(), 
						SortOrientation.ASCENDING.equals(sortSpecifier.getSortDirection()) ? SortOrientation.ASCENDING : SortOrientation.DESCENDING);
			}
		return sp;
	}

	protected TextMatchStyle convertTextMatchStyle(com.smartgwt.client.types.TextMatchStyle textMatchStyle) {

    	if (com.smartgwt.client.types.TextMatchStyle.EXACT.equals(textMatchStyle))
    		return TextMatchStyle.EXACT;
    	else if (com.smartgwt.client.types.TextMatchStyle.STARTS_WITH.equals(textMatchStyle))
    		return TextMatchStyle.STARTS_WITH;
    	else if (com.smartgwt.client.types.TextMatchStyle.SUBSTRING.equals(textMatchStyle))
    		return TextMatchStyle.SUBSTRING;
    	else
    		throw new RuntimeException("Unknown TextMatchStyle " + textMatchStyle.toString());
    }
}
